/**@FILE:OrderBackImpl.java
 * @AUTHOR:qingsheng.zheng
 * @DATE:2015-2-1 上午9:38:35*/
package com.xf.toy.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xf.toy.bean.Express;
import com.xf.toy.bean.OrderBack;
import com.xf.toy.bean.OrderBackExample;
import com.xf.toy.bean.OrderBackExample.Criteria;
import com.xf.toy.bean.Toyorder;
import com.xf.toy.dao.OrderBackMapper;
import com.xf.toy.dao.page.Pagination;
import com.xf.toy.service.ApplyReturnService;
import com.xf.toy.service.ExpressService;
import com.xf.toy.service.OrderBackService;
import com.xf.toy.service.OrderService;
import com.xf.toy.service.ToyService;
import com.xf.toy.service.constant.ToyConst;

/*******************************************
 * @COMPANY:alibaba
 * @CLASS:OrderBackImpl
 * @DESCRIPTION:
 * @AUTHOR:qingsheng.zheng
 * @VERSION:v1.0
 * @DATE:2015-2-1 上午9:38:35
 *******************************************/
@Service("orderBackService")
public class OrderBackServiceImpl implements OrderBackService {
	@Resource
	private OrderBackMapper orderBackMapper;
	@Resource(name = "expressService")
	private ExpressService expressService;
	@Resource(name = "orderService")
	private OrderService orderService;
	@Resource(name = "toyService")
	private ToyService toyService;
	@Resource(name = "applyReturnService")
	private ApplyReturnService applyService;

	@Override
	public void orderback(Long orderid, String rexcode, String lostdesc) {
		Toyorder order = orderService.selectOrderDetail(orderid);
		Express express = expressService.selectExpressByOrderid(orderid);
		OrderBack back = new OrderBack();
		back.setBackTime(new Date());
		back.setRexcode(rexcode);
		back.setBackToToy(ToyConst.TOY_BACK_TOY_N);
		back.setBricklost(lostdesc);
		back.setLastExId(express == null ? null : express.getExpressId());
		back.setToyCode(order.getToyCode());
		back.setUserId(order.getUserId());
		back.setOrderId(order.getOrderId());
		orderBackMapper.insert(back);
		order.setStatus(ToyConst.TOY_ORDER_STATUS_RETURN);
		orderService.updateOrder(order);
		// 找到申请返还的记录，并重置状态
		applyService
				.updateStatusByOrderid(orderid, ToyConst.TOY_APPLY_RETURN_F);

	}

	@Override
	public void shelves(Long backid) {
		OrderBack orderBack = orderBackMapper.selectByPrimaryKey(backid);
		orderBack.setBackToToy(ToyConst.TOY_BACK_TOY_Y);
		orderBackMapper.updateByPrimaryKey(orderBack);
		toyService.toyBack(orderBack.getToyCode(), new Long(1));
	}

	@Override
	public OrderBack selectDetail(Long backid) {
		return orderBackMapper.selectByPrimaryKey(backid);
	}

	@Override
	public List<OrderBack> selectListByStatus(Long backid, String status,
			Pagination page) {
		OrderBackExample backExample = new OrderBackExample();
		if (page != null) {
			backExample.setLimitStart(page.getStart());
			backExample.setLimitEnd(page.getPageSize());
		}
		Criteria criteria = backExample.createCriteria();
		if (backid != null)
			criteria.andBackIdEqualTo(backid);
		criteria.andBackToToyEqualTo(ToyConst.TOY_BACK_TOY_N);
		page.setTotalcount(orderBackMapper.countByExample(backExample));
		return orderBackMapper.selectByExample(backExample);
	}

	@Override
	public List<OrderBack> selectListByStatus(OrderBack back, String status,
			Pagination page) {

		OrderBackExample backExample = new OrderBackExample();
		if (page != null) {
			backExample.setLimitStart(page.getStart());
			backExample.setLimitEnd(page.getPageSize());
		}
		Criteria criteria = backExample.createCriteria();
		if (back.getBackId() != null)
			criteria.andBackIdEqualTo(back.getBackId());
		if (back.getUserId() != null)
			criteria.andUserIdEqualTo(back.getUserId());
		if (back.getToyCode() != null)
			criteria.andToyCodeEqualTo(back.getToyCode());
		criteria.andBackToToyEqualTo(ToyConst.TOY_BACK_TOY_N);
		page.setTotalcount(orderBackMapper.countByExample(backExample));
		return orderBackMapper.selectByExample(backExample);

	}

}
